﻿import pymysql
import time
import os

"""sumary_line
TODO 联表插入
数据库连接
Keyword arguments:
argument -- description
Return: return_description
"""
# 打开数据库连接
db = pymysql.connect(
    host='localhost',
    user='root',
    password='123456',
    database='db_game',
    charset='utf8'
)
cursor = db.cursor()
# sql = """insert into tb_student values(0, "%s", "%s", "110")"""
# data = ('张书豪', '男')
# # data = [
# #         ('张书豪1', '男', '18536475134'),
# #         ('张三1', '男', '18536575134'),
# #         ('貂蝉1', '女', '18436575134'),
# #         ('小乔1', '女', '13436575134')
# #     ]
# try:
#     # cursor.execute(sql)
#     cursor.execute(sql, data)
#     print("主键：{}".format(cursor.lastrowid))
#     db.commit()
# except: 
#     db.rollback()

try:
    start = time.time()
    cursor.execute("""select content from tb_poem where content like '%黄河%'""")
    data = cursor.fetchall()
    with open("{0}/file.txt".format(os.getcwd()), "w", encoding="UTF-8", ) as f:
        for column in data:
            print(column[0])
            f.write(column[0])
            print()
            # print("主键：{}".format(cursor.lastrowid))
    end = time.time()
    print(end - start)
except Exception as e:
    print(e)


###########################
# sql = """insert into tb_poem values(0, %s, %s, %s)"""
# try:
#     poemList = list[tuple[str]]()
#     for i in range(10):
#         poemList.append(("李白{0}".format(i), "床前明月光{0}".format(i), 0))
#     cursor.executemany(sql, poemList)
#     db.commit()
# except Exception as e:
#     db.rollback()
#     print(e)

cursor.close()
db.close()
